<?php
namespace app\api\controller;

use app\api\controller\BaseController;
use think\Db;
use app\api\model\InspectReport as InspectReportModel;
use app\api\model\InspectDistrict as InspectDistrictModel;

class Inspect extends BaseController
{
    public function inspect_report_list($type = '1', $page_index = '1', $page_size = '6')
    {
        // 获取用户信息
        $userInfo = getTokenInfo();
        if (!$userInfo) 
            return base_result(null, false, '10001', '请登录');
        $user_id = $userInfo->user_id;
        // $user_id = 1;
        
        $InspectReportModel = new InspectReportModel;
        $res = $InspectReportModel->where('cons_id', $user_id)->where('type', $type)->order('add_time desc')->page("{$page_index},{$page_size}")->select();
        foreach ($res as $key => $value) {
            // dump($value['id']);
            $ossWhere = ['pid' => $value['id'], 'tab' => 'inspect'];
            $oss_list = Db::table('aliyun_oss')->where($ossWhere)->field('name')->select();
            if ($oss_list) {
                foreach ($oss_list as $k => $val) {
                    $oss_list[$k] = config('ali_cdn_url').'/'.$val['name'].'?'.config('ali_adv_size');
                }
                $res[$key]['image'] = $oss_list;
            }
        }
        // dump(json_decode(json_encode($res)));
        return base_result($res, true);
    }

    /**
     * 地区选择
     * @return [type] [description]
     */
    public function area_list()
    {
        $InspectDistrictModel = new InspectDistrictModel;
        $area = $InspectDistrictModel->distinct(true)->where('status', 1)->field('area_id, area_id as area')->select();
        $district_type = Db::table('inspect_district_type')->where('status', 1)->field('id, name')->select();
        $return = ['area' => $area, 'district_type' => $district_type];
        return base_result($return, true);
    }

    /**
     * 巡查地区列表
     * @param  string $area_id       [description]
     * @param  string $district_type [description]
     * @return [type]                [description]
     */
    public function distinct_list($area_id = "440604", $district_type = '2')
    {
        $InspectDistrictModel = new InspectDistrictModel;
        $whereDis = ['area_id' => $area_id, 'district_type' => $district_type, 'status' => 1];
        $district_list = $InspectDistrictModel->where($whereDis)->field('id, name, area_id as area, district_type, district_type as district')->select();
        foreach ($district_list as $key => $value) {
            $district_list[$key]['title'] = $value['area'] . $value['name'] . $value['district'] . '巡查';
        }
        // dump(json_decode(json_encode($district_list)));die;
        return base_result($district_list, true);
    }

    /**
     * 添加巡查报告
     * @param string $title       [description]
     * @param string $district_id [description]
     * @param string $type        [description]
     * @param string $comment     [description]
     * @param string $imgs        [description]
     */
    public function add_report($title = "", $district_id = "", $type = "", $comment = "", $imgs = "")
    {
        // 获取用户信息
        $userInfo = getTokenInfo();
        if (!$userInfo) 
            return base_result(null, false, '10001', '请登录');
        $user_id = $userInfo->user_id;
        // $user_id = 1;

        if (!$title || !$district_id || !$type) {
            return base_result(null, false, 30002, "参数错误");
        }

        $InspectReportModel = new InspectReportModel;
        $InspectReportModel->data([
            'title' => $title,
            'cons_id' => $user_id,
            'district_id' => $district_id,
            'comment' => $comment,
            'type' => $type,
            'add_time' => date('Y-m-d H:i:s'),
        ]);
        $InspectReportModel->save();
        $id = $InspectReportModel->id;
        if ($type == 2) {
            if ($imgs) {
                $img_data = [];
                foreach ($imgs as $key => $value) {
                    if ($value) {
                        $img_data[$key] = ['name' => $value, 'oss_type' => $type, 'add_time' => date('Y-m-d H:i:s'), 'pid' => $id, 'tab' => 'inspect'];
                    }
                }
                Db::table('aliyun_oss')->insertAll($img_data);
            }
        }
        return base_result(null, true);
    }








}
